package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.iot.domain.IotProductConfig;
import com.ruoyi.iot.service.IIotProductConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品配置Controller
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/iot/product")
public class IotProductConfigController extends BaseController {
    @Autowired
    private IIotProductConfigService iotProductConfigService;

    /**
     * 查询产品配置列表
     */
    @PreAuthorize("@ss.hasPermi('product:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotProductConfig iotProductConfig) {
        startPage();
        List<IotProductConfig> list = iotProductConfigService.selectIotProductConfigList(iotProductConfig);
        return getDataTable(list);
    }


    /**
     * 获取产品配置列表
     *
     * @param iotProductConfig
     * @return
     */
    @GetMapping("/productList")
    public AjaxResult findProductConfigList(IotProductConfig iotProductConfig) {
        List<IotProductConfig> productConfigList = iotProductConfigService.selectIotProductConfigList(iotProductConfig);
        return AjaxResult.success(productConfigList);
    }

    /**
     * 导出产品配置列表
     */
    @PreAuthorize("@ss.hasPermi('product:config:export')")
    @Log(title = "产品配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProductConfig iotProductConfig) {
        List<IotProductConfig> list = iotProductConfigService.selectIotProductConfigList(iotProductConfig);
        ExcelUtil<IotProductConfig> util = new ExcelUtil<IotProductConfig>(IotProductConfig.class);
        util.exportExcel(response, list, "产品配置数据");
    }

    /**
     * 获取产品配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:config:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(iotProductConfigService.selectIotProductConfigByProductId(productId));
    }

    /**
     * 新增产品配置
     */
    @PreAuthorize("@ss.hasPermi('product:config:add')")
    @Log(title = "产品配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody IotProductConfig iotProductConfig) {
        iotProductConfig.setCreateBy(getUsername());
        return toAjax(iotProductConfigService.insertIotProductConfig(iotProductConfig));
    }

    /**
     * 修改产品配置
     */
    @PreAuthorize("@ss.hasPermi('product:config:edit')")
    @Log(title = "产品配置", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody IotProductConfig iotProductConfig) {
        iotProductConfig.setUpdateBy(getUsername());
        return toAjax(iotProductConfigService.updateIotProductConfig(iotProductConfig));
    }

    /**
     * 删除产品配置
     */
    @PreAuthorize("@ss.hasPermi('product:config:remove')")
    @Log(title = "产品配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(iotProductConfigService.deleteIotProductConfigByProductIds(productIds));
    }
}
